//---------------------------------------------------------------------------------
// Copyright (c) 2011, Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//---------------------------------------------------------------------------------

namespace Microsoft.Samples.ServiceBus.WindowsPhoneChat.ViewModels
{
    using System;
    using System.Windows;
    using Microsoft.Samples.ServiceBus.WindowsPhoneChat.Commands;

    public class MainPageViewModel : ViewModel
    {
        public static readonly DependencyProperty ShowAppBarProperty =
            DependencyProperty.Register("ShowAppBar", typeof(bool), typeof(MainPageViewModel), new PropertyMetadata(false));

        private readonly LoginControlViewModel loginControlViewModel;
        private readonly ChatTopicListControlViewModel chatTopicListControlViewModel;

        private bool showAppBar;

        public MainPageViewModel()
        {
            this.loginControlViewModel = new LoginControlViewModel();
            this.loginControlViewModel.LoginCompleted += this.OnLoginCompleted;

            this.chatTopicListControlViewModel = new ChatTopicListControlViewModel();
        }

        public event EventHandler AppBarVisibilityChanged;

        public event ShowDialogEventHandler DialogShown;

        public bool ShowAppBar
        {
            get
            {
                return this.showAppBar;
            }

            set
            {
                if (this.showAppBar != value)
                {
                    this.showAppBar = value;
                    if (this.AppBarVisibilityChanged != null)
                    {
                        this.AppBarVisibilityChanged(this, EventArgs.Empty);
                    }
                }
            }
        }

        public LoginControlViewModel LoginControlViewModel
        {
            get { return this.loginControlViewModel; }
        }

        public ChatTopicListControlViewModel ChatTopicListControlViewModel
        {
            get { return this.chatTopicListControlViewModel; }
        }

        public void BeginRefresh()
        {
            if (Commands.RefreshTopics.CanExecute(this.chatTopicListControlViewModel))
            {
                Commands.RefreshTopics.Execute(this.chatTopicListControlViewModel);
            }
        }

        public void BeginCreate()
        {
            if (Commands.AddTopic.CanExecute(this))
            {
                Commands.AddTopic.Execute(this);
            }
        }

        public void ShowDialog(string dialogName, ViewModel dialogViewModel, DialogCallback callback, object state)
        {
            if (this.DialogShown != null)
            {
                this.DialogShown(this, new ShowDialogEventArgs(dialogName, dialogViewModel, callback, state));
            }
            else
            {
                callback(true, dialogViewModel, state);
            }
        }

        private void OnLoginCompleted(object sender, AsyncCommandEventArgs<LoginControlViewModel> e)
        {
            if (!e.Cancelled)
            {
                this.State = "Chat";
                this.ShowAppBar = true;

                if (Commands.RefreshTopics.CanExecute(this.ChatTopicListControlViewModel))
                {
                    Commands.RefreshTopics.Execute(this.ChatTopicListControlViewModel);
                }
            }
        }
    }
}
